<template>
  <div class="search-result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="$route.query.keyword + '的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item,index) in resultLists"
        :key="index"
        :title="item.title"
        @click="$router.push('/article/'+ item.art_id.toString())"
      />
      <!-- article参数明天复习完之后加 -->
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { reqGetSearchResults } from '@/api/search'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, // 页数
      per_page: 10,
      resultLists: [] // 搜索结果的数据
    }
  },

  methods: {
    async onLoad () {
      // 发送请求
      const { data: { results } } = await reqGetSearchResults(this.$route.query.keyword, this.page, this.per_page)
      console.log(results)
      this.resultLists.push(...results) // 展开追加
      // 手动将laoding关闭
      this.loading = false
      // 更新页码数
      this.page++
      // 处理没有更多了
      if (results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
}
</style>
